package org.ssi.web.result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * 功能概述：<br>
 * FusionChart图表的节点。节点由3部分组成：①节点name ②属性 ③子节点
 * @author 郭艳红
 */
public class ChartNode implements XMLNode {

	/**   */
	private static final long serialVersionUID = 1L;
	
	/** 节点名称  */
	private String name = null;
	
	/** 属性名—属性值 */
	private Map<String, String> attributes = new HashMap<String, String>();
	
	private List<ChartNode> children = new ArrayList<ChartNode>();
	
	public ChartNode(){}
	
	public ChartNode(String name){
		this.name = name;
	}
	
	
	/**
	 *
	 * @see com.epipe.adp.support.XMLNode#toXMLNode(org.w3c.dom.Document)
	 */
	public Node toXMLNode(Document document) {
		
		/** 生成节点主体 */
		Element element = document.createElement(name);
		
		/** 为节点添加attribute */
		Iterator<String> iter = attributes.keySet().iterator();
		String attribute = null;
		while(iter.hasNext()){
			attribute = iter.next();
			element.setAttribute(attribute, attributes.get(attribute));
		}
		
		/** 为节点添加子节点 */
		Iterator<ChartNode> childIter = children.iterator();
		while(childIter.hasNext()){
			element.appendChild(childIter.next().toXMLNode(document));
		}

		return element;
	}
	
	
	/**
	 * 进行默认外观的设置。只能用在chart节点。<br/>
	 * 包括 rotateYAxisName:0  palette:2  useRoundEdges:1   baseFontSize:12   animation:0  showValues:0  formatNumberScale:0
	 */
	public void applyDefaultRenderView(){
		this.setAttribute("rotateYAxisName", "0");
		this.setAttribute("palette", "2");
		this.setAttribute("useRoundEdges", "1");
		this.setAttribute("baseFontSize", "12");
		this.setAttribute("showValues", "0");
		this.setAttribute("formatNumberScale", "0");
	}
	
	
	/**
	 * 获取节点某一属性的值
	 * @param attName 属性名
	 * @return
	 */
	public String getAttribute(String attName){
		return attributes.get(attName);
	}
	
	
	/**
	 * 为节点设置属性
	 * @param attName 属性名
	 * @param attValue 属性值
	 */
	public void setAttribute(String attName, String attValue){
		attributes.put(attName, attValue);
	}
	
	
	/**
	 * 添加子节点
	 * @param child
	 */
	public void addChild(ChartNode child){
		children.add(child);
	}
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	public List<ChartNode> getChildren() {
		return children;
	}

	public void setChildren(List<ChartNode> children) {
		this.children = children;
	}
}

